/*
 * Copyright (C) 2010 Denis Mingulov.
 *
 * This file is part of QReader QtWebView test.
 *
 * Author: Denis Mingulov <denis@mingulov.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */


#include "filelistmodel.h"
#include <QApplication>
#include <QPalette>
#include <QBrush>
#include <QDir>
#include <QResource>
#include <QIcon>

FileListModel::FileListModel(QObject *parent)
    : QAbstractListModel(parent)
{

    // font must be created otherwise on Symbian list drawing will be very slow
    itemFont.setStyle(QFont::StyleNormal);
    itemFont.setStyleHint(QFont::System);
//    itemFont.setStyleStrategy(QFont::PreferDevice | QFont::PreferQuality);
    itemFont.setPointSize(-1);

    // generate icon list
    icons[0] = QIcon(":/icon/icon.svg");
}

int FileListModel::rowCount(const QModelIndex &/*parent*/) const
{
    return fileCount;
}

QVariant FileListModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (index.row() >= fileList.size() || index.row() < 0)
        return QVariant();

    switch(role)
    {
    case Qt::DecorationRole:
        return icons[0];
    case Qt::DisplayRole:
        return fileList.at(index.row()).fileName();
    case Qt::BackgroundRole:
        {
/*            int batch = (index.row() % 2) % 2;
            if (batch == 0)
                return qApp->palette().base();
            else
                return qApp->palette().alternateBase();*/
        }
        break;
    case Qt::ToolTipRole:
        return QString::number(fileList.at(index.row()).size());
    case Qt::FontRole:
        return itemFont;
    default:
        break;
    }

    return QVariant();
}

bool FileListModel::canFetchMore(const QModelIndex &/*index*/) const
{
    if (fileCount < fileList.size())
        return true;
    else
        return false;
}

void FileListModel::fetchMore(const QModelIndex &/*index*/)
{
    int remainder = fileList.size() - fileCount;
    int itemsToFetch = qMin(100, remainder);

    beginInsertRows(QModelIndex(), fileCount, fileCount+itemsToFetch);

    fileCount += itemsToFetch;

    endInsertRows();

    emit numberPopulated(itemsToFetch);
}

void FileListModel::setDirPath(const QString &path)
{
    QDir dir(path);
    pathUsed = dir.canonicalPath();

    // show dirs + files
    dir.setFilter(QDir::AllDirs | QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
    dir.setSorting(QDir::DirsFirst | QDir::IgnoreCase | QDir::LocaleAware | QDir::Name);

    QStringList filters;
//    filters << "*.zip" << "*.fb2" << "*.txt";
    dir.setNameFilters(filters);

    fileList = dir.entryInfoList();

    // check - is it root folder or not? (is it possible to go up?)
    if(!dir.isRoot())
    {
        // add "up"
        fileList.push_front(QFileInfo(".."));
    }

    fileCount = 0;

    reset();

    emit directoryIsChanged(pathUsed);
}

void FileListModel::itemClicked(const QModelIndex &index)
{
    // check what to do
    if (!index.isValid())
        return;

    if (index.row() >= fileList.size() || index.row() < 0)
        return;

    const QFileInfo &file = fileList.at(index.row());
    if(file.fileName().compare("..")==0)
    {
        QDir dir(pathUsed);
        dir.cdUp();
        setDirPath(dir.absolutePath());
    }
    else if(file.isDir())
    {
        QDir dir(pathUsed);
        dir.cd(file.fileName());
        setDirPath(dir.absolutePath());
    }
    else
    {
        // file is selected?
        QDir dir(pathUsed);

        emit fileIsSelected(dir.absoluteFilePath(file.fileName()));
    }

    reset();
}
